package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
@Slf4j
public class ValidateCodeController {

	@Autowired
	JedisPool jedisPool;

	@RequestMapping("/send4Login")
	public Result send4Login(String telephone) throws ClientException {

		//验证码
		Integer validateCode = ValidateCodeUtils.generateValidateCode(6);

		//发送验证码
		//SMSUtils.sendShortMessage(telephone, String.valueOf(validateCode));
		log.debug("发送验证码成功validateCode==="+validateCode);


		//将验证码存储到redis  参数: key  时间(单位秒)   验证码
		jedisPool.getResource().setex(RedisConst.SENDTYPE_LOGIN + "-" + telephone, 5 * 60, String.valueOf(validateCode));

		return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
	}

	@RequestMapping("/send4Order")
	public Result send4Order(String telephone) throws ClientException {

		//验证码
		Integer validateCode = ValidateCodeUtils.generateValidateCode(6);

		//发送验证码
		//SMSUtils.sendShortMessage(telephone, String.valueOf(validateCode));
		log.debug("发送验证码成功validateCode==="+validateCode);


		//将验证码存储到redis  参数: key  时间(单位秒)   验证码
		jedisPool.getResource().setex(RedisConst.SENDTYPE_ORDER + "-" + telephone, 5 * 60, String.valueOf(validateCode));

		return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
	}



}
